local smpl_male `"sex==1"'
local smpl_female `"sex==2"'
local smpl_nbh `"blk==0&hsp==0"'
local smpl_bh `"blk==1|hsp==1"'

local ctrl_male AFQT3 AFQT3_a AFQT3_b age age_a age_b blk hsp nsibs locE locU urban i.hgc_m i.hgc_f i.yob i.divisiona
local ctrl_female `ctrl_male'
local ctrl_nbh AFQT3 AFQT3_a AFQT3_b age age_a age_b female nsibs locE locU urban i.hgc_m i.hgc_f i.yob i.divisiona
local ctrl_bh AFQT3 AFQT3_a AFQT3_b age age_a age_b hsp female nsibs locE locU urban i.hgc_m i.hgc_f i.yob i.divisiona

foreach g in male female nbh bh{
	use ../processed/NLSY79.dta, clear
	
	gen AFQT3_a = max(AFQT3-1/3,0)
	gen AFQT3_b = max(AFQT3-2/3,0)
	gen age_a = max(age-35,0)
	gen age_b = max(age-45,0)
	gen female = sex==2
	
	keep if `smpl_`g''
	local ctrl `ctrl_`g''
	
	local iv pub4 tui4 locE17 locU17
	
	local xlist
	forvalues j=9/18{
		gen x`j' = (hgc_r>=`j')
		local xlist `xlist' x`j'
	}
	
	reg hgc_r `ctrl' [aw=wgt]
	predict xres, resid
	reg hgc_r `iv' `ctrl' [aw=wgt]
	predict xhat, xb
	reg xhat `ctrl' [aw=wgt]
	predict zres, resid
	
	gen AFQT_q = 1
	replace AFQT_q = 2 if AFQT3>(1/3)
	replace AFQT_q = 3 if AFQT3>(2/3)
	forvalues j=1/3{
		gen AFQT_q`j' = AFQT_q==`j'
	}
	gen hgc_p = max(hgc_m,hgc_f)
	gen hgc_p1 = (hgc_p<=2) // neither graduates from high school
	gen hgc_p2 = (hgc_p==3) // at least one graduates from high school but neither attend college
	gen hgc_p3 = (hgc_p>=4) // at least one attends college
	
	local wlist AFQT_q1 AFQT_q2 AFQT_q3 hgc_p1 hgc_p2 hgc_p3
	matrix A = J(8,8,.)
	mat rownames A = `wlist' hs coll
	mat colnames A = w_pop w_pop_se w_ols w_ols_se w_iv w_iv_se b_ols b_ols_se
	// OLS and IV weight on group of covariates
	local j=0
	qui foreach v in `wlist'{
		local j=`j'+1
		gen w_`v' = `v'*xres
		reg `v' [pw=wgt]
		matrix A[`j',1] =  _b[_cons]
		matrix A[`j',2] = _se[_cons]	
		regress w_`v' xres [pw=wgt], vce(cluster cty_grp) nocons
		matrix A[`j',3] =  _b[xres]
		matrix A[`j',4] = _se[xres]
		ivregress 2sls w_`v' (xres=zres) [pw=wgt], vce(cluster cty_grp) nocons
		matrix A[`j',5] =  _b[xres]
		matrix A[`j',6] = _se[xres]	
		reg logwage hgc_r `ctrl' [pw=wgt] if `v'==1, vce(cluster cty_grp) 
		matrix A[`j',7] =  _b[hgc_r]
		matrix A[`j',8] = _se[hgc_r]	
	}
	gen dH = hgc_r<=12
	gen dC = hgc_r>12
	gen xH = min(hgc_r-8,4) // I{x>=9}+..+I{x>=12}
	gen xC = max(hgc_r-12,0) // I{x>=13}+...+I{x>=18}
	gen gH = hgc_r<=12
	gen gC = hgc_r>=12	
	qui foreach v in H C{
		local j=`j'+1
		reg d`v' [pw=wgt]
		matrix A[`j',1] =  _b[_cons]
		matrix A[`j',2] = _se[_cons]	
		regress x`v' hgc_r `ctrl' [pw=wgt], vce(cluster cty_grp)
		matrix A[`j',3] =  _b[hgc_r]
		matrix A[`j',4] = _se[hgc_r]
		ivregress 2sls x`v' (hgc_r=`iv') `ctrl' [pw=wgt], vce(cluster cty_grp)
		matrix A[`j',5] =  _b[hgc_r]
		matrix A[`j',6] = _se[hgc_r]	
		reg logwage hgc_r `ctrl' [pw=wgt] if g`v'==1, vce(cluster cty_grp) 
		matrix A[`j',7] =  _b[hgc_r]
		matrix A[`j',8] = _se[hgc_r]	
	}
	clear
	svmat A, names(col)
	gen grp = "a"
	local i=0
	foreach v in `wlist' hs coll{
		local i = `i'+1
		replace grp = `"`v'"' in `i'
	}
	order grp, first
	export delimited using ../result/wgt_`g'.csv, replace
}
